package uk.ac.lkl.lpp.ui.xml;



import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import uk.ac.lkl.lpp.PedagogyPlan;

import uk.ac.lkl.lpp.xml.ElementProcessor;
import uk.ac.lkl.lpp.ui.xml.LayoutProcessor;
import uk.ac.lkl.lpp.ui.PedagogyPlanUI;
import uk.ac.lkl.lpp.ui.xml.UIElementProcessor;
import uk.ac.lkl.lpp.xml.XMLException;


public class XMLPedagogyPlanUIProcessor extends ElementProcessor {

    private ArrayList<UIElementProcessor> elementProcessors;


    public XMLPedagogyPlanUIProcessor() {
        super("PedagogyPlanView");
        elementProcessors = new ArrayList<UIElementProcessor>();
        createElementProcessors();
    }


    /**
     * Todo: have this in a configuration file in XML.
     * 
     * (XML to describe how the xml should be processed.)
     * 
     */
    private void createElementProcessors() {
        elementProcessors.add(new LayoutProcessor());
    }


    @Override
    public void convertDataToElements(PedagogyPlan plan, Document document,
            Element pedagogyPlanElement) {
        // some redundancy with ViewElementProcessor here but need
        // re-entry into convertElements with appropriate type.
        PedagogyPlanUI view = plan.getUI();
        Element pedagogyPlanModelElement = document
                .createElement("PedagogyPlanView");

        for (UIElementProcessor elementProcessor : elementProcessors)
            elementProcessor.convertDataToElements(view, document,
                    pedagogyPlanModelElement);

        pedagogyPlanElement.appendChild(pedagogyPlanModelElement);
    }


    public void convertElementToData(PedagogyPlan plan, Element element)
            throws XMLException {
        for (UIElementProcessor elementProcessor : elementProcessors)
            elementProcessor.convertElementsToData(plan, element);

    }

}
